﻿using StashyLib;
using System.Web.Mvc;
using Stashy.Models;

namespace Stashy.Controllers
{ 
    public class TaskController : Controller
    {

        private IStashy<int> db;

        public TaskController(IStashy<int> stashy)
        {
            this.db = stashy;
        }
        //
        // GET: /Task/

        public ViewResult Index()
        {
            return View(db.LoadAll<Task>());
        }

        //
        // GET: /Task/Details/5

        public ViewResult Details(int id)
        {
            Task task = db.Load<Task>(id);
            return View(task);
        }

        //
        // GET: /Task/Edit/5
 
        public ActionResult Edit(int? id)
        {
            Task task;
            if (id == null)
            {
                task = new Task() { Id = db.GetNewId<Task>() };
            }
            else
            {
                task = db.Load<Task>(id.Value);
            }

            return View(task);
        }

        //
        // POST: /Task/Edit/5

        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {
                db.Save<Task>(task, task.Id);
                return RedirectToAction("Index");
            }
            return View(task);
        }

        //
        // GET: /Task/Delete/5
 
        public ActionResult Delete(int id)
        {
            Task task = db.Load<Task>(id);
            return View(task);
        }

        //
        // POST: /Task/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            db.Delete<Task>(id);
            return RedirectToAction("Index");
        }
    }
}